home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 2.6 KB | 90 lines |
- // ServerDevice.java
- // A device driver for getting information about and controlling the server.
- import java.util.Vector;
- import java.io.IOException;
-
- public class ServerDevice extends DeviceDriver
- {
- // read
- // Reading from the Server device returns server information, based
- // on the Command header. Valid commands are:
- // 'Netstat' // get active connections
- // 'Properties' // get all server properties
- Message read(Message msg, ServerClient s) throws IOException
- {
- String command = msg.find("Command");
- if (command == null)
- throw new IOException("No Command given");
-
- Message r = new Message();
- r.add("Reply","Data");
- if (command.equals("Netstat")) {
- // Send back a list of active connections
- BufferOutputStream buf = new BufferOutputStream();
- Vector cons = JFSserver.cons;
- synchronized(cons) {
- for(int i=0; i<cons.size(); i++) {
- ServerClient c=(ServerClient)cons.elementAt(i);
- StringJoiner j = new StringJoiner(':');
- j.add(String.valueOf(c.cnum));
- j.add(c.host);
- j.add(c.name==null ? "" : c.name);
- j.add(String.valueOf(c.last));
- j.add(String.valueOf(c.sent));
- j.add(String.valueOf(c.recv));
- buf.puts(j.toString());
- }
- }
- r.setdata(buf.getarray());
- }
- else if (command.equals("Properties")) {
- // Send a list of all server properties
- ServerProperty allp[] = JFSserver.allprops();
- BufferOutputStream buf = new BufferOutputStream();
- for(int i=0; i<allp.length; i++)
- buf.puts(allp[i].output());
- r.setdata(buf.getarray());
- }
- else
- throw new IOException("Unknown command : "+command);
- return r;
- }
-
- // write
- // Writing to the Server device performs some action based on the
- // Command header, and other headers specific to certain commands.
- // Valid commands are:
- // 'Kill' // Disconnect a client. Required the Id header
- void write(Message msg, ServerClient s) throws IOException
- {
- String cmd = msg.find("Command");
- if (cmd == null)
- throw new IOException("No Command given");
-
- // Carry out command
- if (cmd.equals("Kill")) {
- // Kill an existing connection
- String idstr = msg.find("Id");
- if (idstr == null)
- throw new IOException("No Id given");
- int id = Integer.parseInt(idstr);
- ServerClient idc = null;
- for(int i=0; i<JFSserver.cons.size(); i++) {
- ServerClient c =
- (ServerClient)JFSserver.cons.elementAt(i);
- if (c.cnum == id) idc = c;
- }
- if (idc == null)
- throw new IOException("Connection Id not found");
- if (idc == s)
- throw new IOException("Cannot kill own connection");
- idc.shutdown();
- }
- else {
- // Unknown command
- throw new IOException("Command "+cmd+" not found");
- }
- }
- }
-
-